/*
 *  Copyright (C) 2010 Martin Haulrich <mwh.isv@cbs.dk> and Matthias Buch-Kromann <mbk.isv@cbs.dk>
 *
 *  This file is part of the IncrementalParser package.
 *
 *  The IncrementalParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.osdtsystem.incparser.data;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Martin Haulrich
 */
public class IO {
    private static void writeToCONLLFile(PrintWriter out, CONLLSentence sen) throws IOException {

        for (CONLLToken token : sen.words) {
            StringBuilder sb = new StringBuilder();
//            System.err.println(token.get(CONLLToken.FORM));
            for (String t : token.tokens) {

                sb.append(t).append("\t");
            }
            // Remove trailing \t
            sb.deleteCharAt(sb.length() - 1);
            out.println(sb.toString());
        }
        out.println();
    }

    public static void writeCONLLFile(String fn, Iterator<CONLLSentence> iterator, List<List<Integer>> headsList) throws IOException {

        PrintWriter out = new PrintWriter(fn, "UTF8");
        int i = 0;
        while (iterator.hasNext()) {
            CONLLSentence sen = iterator.next();
            writeToCONLLFile(out, addSolution(sen, headsList.get(i)));
            i++;
        }
        out.close();
    }

    public static void writeCONLLFile(String fn, Iterator<CONLLSentence> iterator, List<List<Integer>> headsList, List<List<String>> labels) throws IOException {

        PrintWriter out = new PrintWriter(fn, "UTF8");
        int i = 0;
        while (iterator.hasNext()) {
            CONLLSentence sen = iterator.next();

            writeToCONLLFile(out, addSolution(sen, headsList.get(i), labels.get(i)));
            i++;
        }
        out.close();
    }



    public static CONLLSentence addSolution(CONLLSentence sen, List<Integer> heads) {
        List<CONLLToken> newTokens = new ArrayList<CONLLToken>();

        for (int i = 1; i < sen.size(); i++) {
            CONLLToken token = sen.get(i);
//            System.err.println(token.get(CONLLToken.FORM));
            CONLLToken newToken = new CONLLToken(new ArrayList<String>(token.tokens));
            newToken.tokens.set(CONLLToken.DEPREL, "<no-type>");
            newToken.tokens.set(CONLLToken.HEAD, "" + heads.get(i));
            newTokens.add(newToken);

        }

        return new CONLLSentence(newTokens);
    }

    public static CONLLSentence addSolution(CONLLSentence sen, List<Integer> heads, List<String> labels) {
        List<CONLLToken> newTokens = new ArrayList<CONLLToken>();

        for (int i = 1; i < sen.size(); i++) {
            CONLLToken token = sen.get(i);
//            System.err.println(token.get(CONLLToken.FORM));
            CONLLToken newToken = new CONLLToken(new ArrayList<String>(token.tokens));
            newToken.tokens.set(CONLLToken.DEPREL, labels.get(i));
            newToken.tokens.set(CONLLToken.HEAD, "" + heads.get(i));
            newTokens.add(newToken);

        }

        return new CONLLSentence(newTokens);
    }
}
